package com.heima.behavior.listener;

import com.alibaba.fastjson.JSON;
import com.heima.behavior.service.ApFollowBehaviorService;
import com.heima.model.behavior.dtos.FollowBehaviorDto;
import com.heima.model.common.constants.FollowBehaviorConstants;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Optional;

@Component
@Slf4j
public class FollowBehaviorListener {
    @Autowired
    ApFollowBehaviorService apFollowBehaviorService;

    @KafkaListener(topics = FollowBehaviorConstants.FOLLOW_BEHAVIOR_TOPIC)
    public void receiverMessage(ConsumerRecord<?,?>record){
        if (Optional.ofNullable(record).isPresent()){
            log.info("FollowBehaviorListener receiverMessage：{}", record.value());
            FollowBehaviorDto dto = JSON.parseObject((String) record.value(), FollowBehaviorDto.class);
            apFollowBehaviorService.saveFollowBehavior(dto);
            log.info("***************FollowBehaviorListener success******************");
        }
    }
}
